package com.example.bookstore.controller;

import com.example.bookstore.common.BaseContext;
import com.example.bookstore.common.PageResult;
import com.example.bookstore.common.Result;
import com.example.bookstore.dto.AppraiseRequest;
import com.example.bookstore.dto.QueryCartParamsDto;
import com.example.bookstore.entity.Collect;
import com.example.bookstore.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    CollectService collectService;

    @PutMapping("/{id}")
    public Result<String> addCollect(@PathVariable Integer id) {
        Collect collect = new Collect();
        collect.setBookId(id);
        collect.setUserId(BaseContext.getCurrentId());
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        collect.setAddTime(format);
        collectService.save(collect);

        return Result.success("收藏成功");
    }
    @PostMapping("/{id}")
    public Result<String> isCollected(@PathVariable Integer id) {
        int userId = BaseContext.getCurrentId();
        Result<String> stringResult = collectService.isCollected(id,userId);
        return stringResult;
    }

    @PostMapping("/page")
    public PageResult<Collect> selectCollect(int pageSize, int currentPage, @RequestBody QueryCartParamsDto params) {
        return collectService.selectcollect(pageSize, currentPage, params);
    }


    @DeleteMapping("/{id}")
    public Result deleteCollect(@PathVariable Integer id) {
        int userId = BaseContext.getCurrentId();
        return collectService.deleteCollect(id,userId);
    }
}
